﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ObTookit.Auto
{
    public class BaseCoder
    {
        public string Template;
        public string Author;
        public string CreateDate;
        public AutoObject AutoObj;
        public List<FieldObject> FieldObjs;

        public string ReadTemplate(string fileName)
        {
            try
            {
                string filePath = System.AppDomain.CurrentDomain.BaseDirectory;
                filePath = filePath + "Template/" + fileName;
                if (!File.Exists(filePath))
                {
                    MessageBox.Show("模板文件[" + fileName + "]不存在！");
                    return null;
                }
                string text = File.ReadAllText(filePath, System.Text.Encoding.Default);
                return text;
            }
            catch(Exception ex)
            {
                MessageBox.Show("读取模板文件[" + fileName + "]异常：" + ex.Message + "\r\n" + ex.StackTrace);
                return null;
                throw ex;
            }
        }

        public FieldObject GetIdObject()
        {
            FieldObject fobj = null;
            foreach (FieldObject fo in FieldObjs)
            {
                if (fo.PrimaryKey)
                {
                    fobj = fo;
                }
            }
            //
            if (fobj == null)
            {
                MessageBox.Show("主键属性实体为NULL，没有设置主键！！");
                fobj = new FieldObject();
                fobj.Type = "IntField";
            }

            return fobj;
        }

        public string GetListType(string type)
        {
            return type.Replace("Field", "") + "ListField";
        }

        public string GetTryGetIdType(string type)
        {
            switch(type)
            {
                case "LongField":
                    return "TryGetInt64";
                case "IntField":
                default:
                    return "TryGetInt32";
            }

        }

        public string GetTryGetType(string type)
        {
            switch (type)
            {
                case "LongField":
                    return "TryGetLong";
                case "IntField":
                default:
                    return "TryGetInt";
            }
        }
        public string GetTryGetFirstType(string type)
        {
            switch (type)
            {
                case "LongField":
                    return "TryGetFirstLong";
                case "IntField":
                default:
                    return "TryGetFirstInt";
            }
        }

        public string GetIdType(string type)
        {
            switch(type)
            {
                case "LongField":
                    return "long";
                case "IntField":
                default:
                    return "int";
            }
        }

    }
}
